package com.sprouting.ops.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sprouting.ops.annotations.invoke.IdempotentSingle;
import com.sprouting.ops.interfaces.AddGroup;
import com.sprouting.ops.interfaces.ConstantBase;
import com.sprouting.ops.interfaces.UpdGroup;
import com.sprouting.ops.model.OpsSysDictGroup;
import com.sprouting.ops.model.ResultJson;
import com.sprouting.ops.model.bo.OpsSysDictGroupBo;
import com.sprouting.ops.model.qo.OpsSysDictGroupQo;
import com.sprouting.ops.service.OpsSysDictGroupService;
import com.sprouting.ops.utils.CheckTool;
import com.sprouting.ops.utils.EntityTool;
import com.sprouting.ops.utils.IdTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 字典组;(OpsSysDictGroup)(OPS_SYS_DICT_GROUP) 表控制层
 *
 * @author ：lx（长沙麓谷）
 * @date 创建时间：2025-08-14 20:58:14
 */
@Slf4j
@RestController
@RequestMapping("opsSysDictGroup")
public class OpsSysDictGroupController {

    @Resource
    private OpsSysDictGroupService opsSysDictGroupService;




    /**
     * 分页查询字典组
     * @param query 查询参数
     * @return
     */
    @PostMapping("getListPage")
    public ResultJson getListPage(OpsSysDictGroupQo query){
        Page page = query.getPage();
        return ResultJson.success(opsSysDictGroupService.getListPage(page, query));
    }

    /**
     * 删除字典组
     * @param id 字典组;id
     * @return
     */
    @PostMapping("delOpsSysDictGroup")
    @IdempotentSingle
    public ResultJson delOpsSysDictGroup(String id){
        if (CheckTool.checkNull(id)){
            return ResultJson.errorCheck();
        }

        OpsSysDictGroup db = opsSysDictGroupService.getById(id);
        if (db == null){
            return ResultJson.success();
        }
        if (!ConstantBase.BaseData.NO.equals(db.getBaseData())){
            return ResultJson.error("基础数据不允许操作");
        }

        opsSysDictGroupService.delOpsSysDictGroup(id);
        return ResultJson.success();
    }

    /**
     * 更新字典组
     * @param bo 修改参数
     * @return
     */
    @PostMapping("updOpsSysDictGroup")
    @IdempotentSingle
    public ResultJson updOpsSysDictGroup(@Validated(UpdGroup.class) OpsSysDictGroupBo bo){
        String msg = checkOpsSysDictGroup(bo);
        if (msg != null){
            return ResultJson.error(msg);
        }

        OpsSysDictGroup db = opsSysDictGroupService.getById(bo.getId());
        if (db == null){
            return ResultJson.errorCheck();
        }
        if (ConstantBase.BaseData.YES_BASE.equals(db.getBaseData())){
            return ResultJson.error("基础数据不允许修改");
        }

        OpsSysDictGroup sysDictGroup = BeanUtil.copyProperties(bo, OpsSysDictGroup.class);
        EntityTool.updBase(sysDictGroup);
        opsSysDictGroupService.updateById(sysDictGroup);
        return ResultJson.success(bo);
    }

    /**
     * 保存字典组
     * @param bo 保存参数
     * @return
     */
    @PostMapping("saveOpsSysDictGroup")
    @IdempotentSingle
    public ResultJson saveOpsSysDictGroup(@Validated(AddGroup.class) OpsSysDictGroupBo bo){
        String msg = checkOpsSysDictGroup(bo);
        if (msg != null){
            return ResultJson.error(msg);
        }

        OpsSysDictGroup sysDictGroup = BeanUtil.copyProperties(bo, OpsSysDictGroup.class);
        sysDictGroup.setId(IdTool.getId());
        if (sysDictGroup.getBaseData() == null){
            sysDictGroup.setBaseData(ConstantBase.BaseData.NO);
        }
        if (sysDictGroup.getSort() == null){
            sysDictGroup.setSort(ConstantBase.DefaultValue.SORT);
        }
        EntityTool.saveBase(sysDictGroup);
        opsSysDictGroupService.save(sysDictGroup);
        return ResultJson.success(bo);
    }


    /**
     * 字典组 数据校验
     * @param bo 字典组; 校验数据
     * @return 非null则有错误
     */
    private String checkOpsSysDictGroup(OpsSysDictGroupBo bo){
        OpsSysDictGroup db = opsSysDictGroupService.getByCodeFilterId(bo.getCode(), bo.getId());
        if (db != null){
            return "字典组代码已使用，请勿重复注册";
        }
        return null;
    }

}
